WhirlwindTourOfPython | Jupyter Notebooks behind my OReilly report | Code Editor library
kandi X-RAY | WhirlwindTourOfPython Summary
kandi X-RAY | WhirlwindTourOfPython Summary
The Jupyter Notebooks behind my OReilly report, "A Whirlwind Tour of Python"
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of WhirlwindTourOfPython
WhirlwindTourOfPython Key Features
WhirlwindTourOfPython Examples and Code Snippets
Community Discussions
Trending Discussions on WhirlwindTourOfPython
QUESTION
I am studying generators in python. I follow the code from https://jakevdp.github.io/WhirlwindTourOfPython/12-generators.html but totally confused by the program when n=2
, what exactly is the result of all(n % p > 0 for p in primes)
?
Per my understand, the first round of the loop, primes
is empty. So how come the expression is True and adding 2
to the set?
ANSWER
Answered 2021-Jul-02 at 02:54From the documentation of all()
:
Return
True
if all elements of the iterable are true (or if the iterable is empty)
So when primes
is empty, n % p > 0 for p in primes
is empty because there's nothing to iterate over. Therefore all()
returns True
, and n
is added to primes
.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install WhirlwindTourOfPython
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page